#include <QDir>
#include <QMessageBox>
#include <QTextCodec>
#include <QTextStream>


#include "CONST.h"
#include "fmHelp.h"
#include "ui_fmHelp.h"

using namespace Mikroakustika::MDeviceChecker;

//----------------------------------------------------------------------------------------
fmHelp::fmHelp(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::fmHelp) {

    ui->setupUi(this);

//    QTextDocument *doc = new QTextDocument();

    ui->txtHelp->setReadOnly(true);

    QString fileNameHelp(QDir::toNativeSeparators("help\\help.htm"));

    if (!QFile::exists(fileNameHelp)) {
        ui->txtHelp->setText(tr("Help-file not founded"));
        return;
    }

    QFile file(fileNameHelp);
    if (!file.open(QFile::ReadOnly)) {
        ui->txtHelp->setText(tr("Can't open Help-file"));
        return;
    }

    QByteArray data = file.readAll();
    QTextCodec *codec = Qt::codecForHtml(data);
    QString str = codec->toUnicode(data);
    if (Qt::mightBeRichText(str)) {
        ui->txtHelp->setHtml(str);
    } else {
        str = QString::fromLocal8Bit(data);
        ui->txtHelp->setPlainText(str);
    }

    file.close();
}

//----------------------------------------------------------------------------------------
fmHelp::~fmHelp() {
    delete ui;
}

//----------------------------------------------------------------------------------------
void fmHelp::changeEvent(QEvent *e) {
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//----------------------------------------------------------------------------------------
void fmHelp::on_btnClose_clicked() {
    close();
}
